﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Chat.Common;

public class CtlMsgHandle : IHandleable
{
    public void Handle(string clientId, string cmd)
    {    
        if (cmd.Length >= Common.ctlTypeSize)
        {
            string ctlType = cmd.Substring(0, Common.ctlTypeSize).Trim();
            string param = cmd.Substring(Common.ctlTypeSize, cmd.Length - Common.ctlTypeSize);
            if (string.Compare(ctlType, ControlType.GetRoomList.ToString(), true) == 0)  // 请求获取房间列表
                GetRoomList(clientId);           
            else if (string.Compare(ctlType, ControlType.GetRoomInfo.ToString(), true) == 0)  // 请求获取房间信息
                GetRoomInfo(clientId, param);
            else if (string.Compare(ctlType, ControlType.EnterRoom.ToString(), true) == 0) // 请求进入房间
                EnterRoom(clientId, param);
            else if (string.Compare(ctlType, ControlType.ExitRoom.ToString(), true) == 0) // 请求退出房间
                ExitRoom(clientId);
            else if (string.Compare(ctlType, ControlType.GetOwner.ToString(), true) == 0) // 请求获取自身信息
                GetOwner(clientId);
            else if (string.Compare(ctlType, ControlType.SetOwner.ToString(), true) == 0) // 请求设置自身信息
                SetOwner(clientId,param);
            else if (string.Compare(ctlType, ControlType.GetMemList.ToString(), true) == 0) // 获取成员列表
                GetMemList(clientId);
            else if (string.Compare(ctlType, ControlType.EnterServer.ToString(), true) == 0) // 请求进入服务器
                EnterServer(clientId);
            else if (string.Compare(ctlType, ControlType.ExitServer.ToString(), true) == 0) // 请求退出服务器
                ExitServer(clientId);
            else
                return;
        }
    }
    /// <summary>
    /// 向指定成员发送当前房间列表,如果未给出指定成员,则向所有在线成员发送
    /// </summary>
    /// <param name="clientId">客户Id</param>
    public static void GetRoomList(string clientId=null)
    {
        Debug.Log($"客户{clientId}获取房间列表!");
        string cmd = Singleton<RoomManager>.GetInstance().ToString();
        Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.GetRoomList, clientId,cmd);
    }
    /// <summary>
    /// 向指定成员发送当前在线成员列表，如果未给出指定成员Id，则向所有在线成员发送
    /// </summary>
    /// <param name="clientId">成员Id</param>
    public static void GetMemList(string clientId=null)
    {
        Debug.Log($"客户{clientId}获取成员列表!");
        string cmd = Singleton<MemberManager>.GetInstance().ToString();
        Debug.Log(cmd);
        Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.GetMemList, clientId, cmd);
    }
    public void GetRoomInfo(string clientId,string roomId)
    {
        Debug.Log($"客户{clientId}获取房间信息!");
        string cmd = Singleton<RoomManager>.GetInstance().GetValue(roomId).ToString();
        Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.GetRoomInfo,clientId, cmd);
    }
    public void EnterRoom(string clientId,string roomId)
    {
        Debug.Log($"客户{clientId}想要进入{roomId}房间!");
        Singleton<RoomManager>.GetInstance().GetValue(roomId).Add(Singleton<MemberManager>.GetInstance().GetValue(clientId));
        GetRoomList();
        GetMemList();
    }
    public void ExitRoom(string clientId)
    {
        Debug.Log($"客户{clientId}退出{Singleton<MemberManager>.GetInstance().GetValue(clientId).Room.Id}房间!");
        Singleton<MemberManager>.GetInstance().GetValue(clientId).Room.Remove(clientId);
        GetRoomList();
        GetMemList();
    }
    public static void GetOwner(string clientId)
    {
        Debug.Log($"客户{clientId}获取自身信息!");
        string cmd = Singleton<MemberManager>.GetInstance().GetValue(clientId).ToString();
        Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.GetOwner,clientId, cmd);
    }
    public static void SetOwner(string clientId,string param)
    {
        Debug.Log($"客户{clientId}设置自身信息!");
        Singleton<MemberManager>.GetInstance().GetValue(clientId).BaseClient.Name = param;
        GetOwner(clientId);
        EnterServer(clientId);
        GetMemList();
    }
    public static void EnterServer(string clientId)
    {
        Debug.Log($"客户{clientId}进入服务器!");
        Member member = Singleton<MemberManager>.GetInstance().GetValue(clientId);
        Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.EnterServer, null, member.ToString());
        GetMemList();
    }
    public static void  ExitServer(string clientId)
    {
        Debug.Log($"客户{clientId}退出服务器!");
        Member member = Singleton<MemberManager>.GetInstance().GetValue(clientId);
        Singleton<CommandSender>.GetInstance().SendControlCmd(ControlType.ExitServer, null, member.ToString());
        GetMemList();
    }
}
